package day12_thread_code.com.itheima.d1_thread_create;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class Demo3_Thread {
    public static void main(String[] args) {
        MyCallable call = new MyCallable(100);
        FutureTask<String> task = new FutureTask<>(call);
        Thread t = new Thread(task);
        t.start();

        MyCallable call2 = new MyCallable(200);
        FutureTask<String> task1 = new FutureTask<>(call2);
        Thread t2 = new Thread(task1);
        t2.start();
        try{
            String rs1 = task.get();
            System.out.println("rs1 = " + rs1);
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            String rs2 = task1.get();
            System.out.println("rs2 = " + rs2);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

class MyCallable implements Callable<String>{
    private int n;
    public MyCallable(int n){
        this.n = n;
    }
    @Override
    public String call() throws Exception {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += i;
        }
        return "1到" + n + "的和为：" + sum;
    }
}
